<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="技术博客">
    
    <meta name="author" content="jiesun">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    
    <!--- Seo Part-->
    
    <link rel="canonical" href="https://anca.gitee.io/2023/02/21/reactive/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="vue的响应式系统不仅仅是proxy">
<meta property="og:type" content="article">
<meta property="og:title" content="vue响应式系统">
<meta property="og:url" content="https://anca.gitee.io/2023/02/21/reactive/index.html">
<meta property="og:site_name" content="孙杰的博客">
<meta property="og:description" content="vue的响应式系统不仅仅是proxy">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-02-21T05:59:18.087Z">
<meta property="article:modified_time" content="2023-02-27T08:12:04.068Z">
<meta property="article:author" content="jiesun">
<meta property="article:tag" content="技术博客">
<meta name="twitter:card" content="summary">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="/jiesun.gitee.io/images/redefine-favicon.svg" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="/jiesun.gitee.io/images/redefine-favicon.svg">
    <meta name="theme-color" content="#A31F34">
    <link rel="shortcut icon" href="/jiesun.gitee.io/images/redefine-favicon.svg">
    <!--- Page Info-->
    
    <title>
        
            vue响应式系统 -
        
        极乐净土
    </title>
    
<link rel="stylesheet" href="/jiesun.gitee.io/css/style.css">


    
        
<link rel="stylesheet" href="/jiesun.gitee.io/css/build/styles.css">

    

    
<link rel="stylesheet" href="/jiesun.gitee.io/fonts/fonts.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fonts/Satoshi/satoshi.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fonts/Chillax/chillax.css">

    <!--- Font Part-->
    
    
    
    

    <!--- Inject Part-->
    
    <script id="hexo-configurations">
    let Global = window.Global || {};
    Global.hexo_config = {"hostname":"anca.gitee.io","root":"/jiesun.gitee.io/","language":"zh-CN"};
    Global.theme_config = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":true},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":[]},"code_block":{"copy":true,"style":"mac","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":false,"expand":true,"init_open":true},"copyright":true,"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"mobile_limit":2,"placeholder":"/images/wallhaven-wqery6-light.webp","skip_dirs":[]}},"colors":{"primary":"#A31F34","secondary":null},"global":{"fonts":{"chinese":{"enable":false,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":false,"percentage":true},"website_counter":{"url":"https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js","enable":true,"site_pv":true,"site_uv":true,"post_pv":true},"single_page":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"fixed","image":{"light":"/images/wallhaven-wqery6-light.webp","dark":"/images/wallhaven-wqery6-dark.webp"},"title":"极乐净土","subtitle":{"text":[],"hitokoto":{"enable":false,"api":"https://v1.hitokoto.cn"},"typing_speed":100,"backing_speed":80,"starting_delay":500,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":false,"links":{"github":null,"instagram":null,"zhihu":null,"twitter":null,"email":null},"qrs":{"weixin":null}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":true,"type":"fixed","audios":[{"name":"起风了","artist":"买辣椒也用券","url":"https://music.163.com/song/media/outer/url?id=1330348068.mp3","cover":"https://p2.music.126.net/diGAyEmpymX8G7JcnElncQ==/109951163699673355.jpg?param=130y130"},{"name":"盗墓笔记·十年人间","artist":"青肆ToriDo","url":"https://music.163.com/song/media/outer/url?id=1298484254.mp3","cover":"https://p2.music.126.net/4vY0tPpnINMLzjV8UKywIg==/109951163438805808.jpg?param=130y130"},{"name":"囍","artist":"葛东琪","url":"https://music.163.com/song/media/outer/url?id=1303289043.mp3","cover":"https://p2.music.126.net/H3Q3SeMVtuHvHY2uyaQdOw==/109951163472855051.jpg?param=130y130"}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.4.4","navbar":{"auto_hide":false,"color":{"left":"#f78736","right":"#367df7","transparency":35},"links":{"Home":{"path":"/","icon":"fa-regular fa-house"},"Memos":{"path":"/essay","icon":"fa-regular fa-lightbulb"},"Archives":{"icon":"fa-regular fa-archive","submenus":{"Archives":"/archives","Categories":"/categories","Tags":"/tags"}}},"search":{"enable":false,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"menu","announcement":null,"links":null},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}},"footerStart":"2022/8/17 11:45:14"};
    Global.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
    Global.data_config = {"masonry":false};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/jiesun.gitee.io/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fontawesome/regular.min.css">

    
    
    
    
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
        <span class="swup-progress-icon">
            <i class="fa-solid fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container" id="swup">

    

    <div class="main-content-container">


        <div class="main-content-header">
            <header class="navbar-container">
    
    <div class="navbar-content">
        <div class="left">
            
            <a class="logo-title" href="https://anca.gitee.io/">
                
                极乐净土
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/jiesun.gitee.io/"  >
                                    
                                        
                                            <i class="fa-regular fa-house"></i>
                                        
                                        首页
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/jiesun.gitee.io/essay"  >
                                    
                                        
                                            <i class="fa-regular fa-lightbulb"></i>
                                        
                                        MEMOS
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="has-dropdown" 
                                    href="#" onClick="return false;">
                                    
                                        
                                            <i class="fa-regular fa-archive"></i>
                                        
                                        归档&nbsp;<i class="fa-solid fa-chevron-down"></i>
                                    
                                </a>
                                <!-- Submenu -->
                                
                                    <ul class="sub-menu">
                                    
                                        <li>
                                        <a href="/jiesun.gitee.io/archives">归档
                                        </a>
                                        </li>
                                    
                                        <li>
                                        <a href="/jiesun.gitee.io/categories">分类
                                        </a>
                                        </li>
                                    
                                        <li>
                                        <a href="/jiesun.gitee.io/tags">标签
                                        </a>
                                        </li>
                                    
                                    </ul>
                                
                            </li>
                    
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile drawer -->
    <div class="navbar-drawer w-full absolute top-0 left-0 bg-background-color">
        <ul class="drawer-navbar-list flex flex-col justify-start items-center">
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group " 
                        href="/jiesun.gitee.io/"  >
                             
                                
                                    <i class="fa-regular fa-house"></i>
                                
                                首页
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group " 
                        href="/jiesun.gitee.io/essay"  >
                             
                                
                                    <i class="fa-regular fa-lightbulb"></i>
                                
                                MEMOS
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group has-dropdown" 
                        href="#" onClick="return false;">
                            
                                
                                    <i class="fa-regular fa-archive"></i>
                                
                                归档&nbsp;<i class="group-hover:rotate-180 transition-transform fa-solid fa-chevron-down"></i>
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                              
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/jiesun.gitee.io/archives">归档</a>
                            </li>
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/jiesun.gitee.io/categories">分类</a>
                            </li>
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/jiesun.gitee.io/tags">标签</a>
                            </li>
                        
                    
            

        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="post-page-container">
    <div class="article-content-container">

        <div class="article-title">
            
                
                
                <img src="https://anca.gitee.io/jiesun.gitee.io/images/bg10.jpeg" alt="vue响应式系统" class="max-w-none"/>
                
                <h1 class="article-title-cover">vue响应式系统</h1>
            
            </div>
            
                    
        
        
            <div class="article-header">
                <div class="avatar">
                    <img src="/jiesun.gitee.io/images/redefine-avatar.svg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">jiesun</span>
                        
                            <span class="author-label">Lv3</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2023-02-21 13:59:18</span>
        <span class="mobile">2023-02-21 13:59:18</span>
        <span class="hover-info">创建</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2023-02-27 16:12:04</span>
            <span class="mobile">2023-02-27 16:12:04</span>
            <span class="hover-info">更新</span>
        </span>
    

    
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        


        <div class="article-content markdown-body">
            <ul>
<li>以下代码对源代码做了部分简化，方便理解<br>整个响应式的由几个主要文件组成所有功能<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">├── baseHandlers.ts // 基本类型的处理器</span><br><span class="line">├── collectionHandlers.ts  // Set Map WeakSet WeckMap的处理器</span><br><span class="line">├── computed.ts // 计算属性，同Vue2</span><br><span class="line">├── effect.ts // reactive 核心，处理依赖收集，依赖更新</span><br><span class="line">├── index.ts</span><br><span class="line">├── operations.ts // 定义依赖收集，依赖更新的类型</span><br><span class="line">├── reactive.ts // reactive 入口，内部主要以Proxy实现</span><br><span class="line">└── ref.ts // reactive 的变种方法，Proxy处理不了值类型的响应，Ref来处理</span><br></pre></td></tr></table></figure></div></li>
</ul>
<h2 id="reactive"><a href="#reactive" class="headerlink" title="reactive"></a>reactive</h2><p>由 <code>proxy</code> 和 <code>effect</code> 组合</p>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line">export function reactive&lt;T extends object&gt;(target: T): UnwrapNestedRefs&lt;T&gt;</span><br><span class="line">export function reactive(target: object) &#123;</span><br><span class="line">  // if trying to observe a readonly proxy, return the readonly version.</span><br><span class="line">  // 如果目标对象是一个只读的响应数据,则直接返回目标对象</span><br><span class="line">  if (target &amp;&amp; (target as Target).__v_isReadonly) &#123;</span><br><span class="line">    return target</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // 否则调用  createReactiveObject 创建 observe</span><br><span class="line">  return createReactiveObject(</span><br><span class="line">    target, </span><br><span class="line">    false,</span><br><span class="line">    mutableHandlers,</span><br><span class="line">    mutableCollectionHandlers</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line">// Target 目标对象</span><br><span class="line">// isReadonly 是否只读 </span><br><span class="line">// baseHandlers 基本类型的 handlers</span><br><span class="line">// collectionHandlers 主要针对(set、map、weakSet、weakMap)的 handlers</span><br><span class="line">function createReactiveObject(</span><br><span class="line">  target: Target,</span><br><span class="line">  isReadonly: boolean,</span><br><span class="line">  baseHandlers: ProxyHandler&lt;any&gt;,</span><br><span class="line">  collectionHandlers: ProxyHandler&lt;any&gt;</span><br><span class="line">) &#123;</span><br><span class="line">  // 如果不是对象</span><br><span class="line">  if (!isObject(target)) &#123;</span><br><span class="line">    // 在开发模式抛出警告，生产环境直接返回目标对象</span><br><span class="line">    if (__DEV__) &#123;</span><br><span class="line">      console.warn(`value cannot be made reactive: $&#123;String(target)&#125;`)</span><br><span class="line">    &#125;</span><br><span class="line">    return target</span><br><span class="line">  &#125;</span><br><span class="line">  // target is already a Proxy, return it.</span><br><span class="line">  // exception: calling readonly() on a reactive object</span><br><span class="line">  // 如果目标对象已经是个 proxy 直接返回</span><br><span class="line">  if (target.__v_raw &amp;&amp; !(isReadonly &amp;&amp; target.__v_isReactive)) &#123;</span><br><span class="line">    return target</span><br><span class="line">  &#125;</span><br><span class="line">  // target already has corresponding Proxy</span><br><span class="line">  if (</span><br><span class="line">    hasOwn(target, isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive)</span><br><span class="line">  ) &#123;</span><br><span class="line">    return isReadonly ? target.__v_readonly : target.__v_reactive</span><br><span class="line">  &#125;</span><br><span class="line">  // only a whitelist of value types can be observed.</span><br><span class="line"></span><br><span class="line">  // 检查目标对象是否能被观察, 不能直接返回</span><br><span class="line">  if (!canObserve(target)) &#123;</span><br><span class="line">    return target</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // 使用 Proxy 创建 observe </span><br><span class="line">  const observed = new Proxy(</span><br><span class="line">    target,</span><br><span class="line">    collectionTypes.has(target.constructor) ? collectionHandlers : baseHandlers</span><br><span class="line">  )</span><br><span class="line"></span><br><span class="line">  // 打上相应标记</span><br><span class="line">  def(</span><br><span class="line">    target,</span><br><span class="line">    isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive,</span><br><span class="line">    observed</span><br><span class="line">  )</span><br><span class="line">  return observed</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 同时满足3个条即为可以观察的目标对象</span><br><span class="line">// 1. 没有打上__v_skip标记</span><br><span class="line">// 2. 是可以观察的值类型</span><br><span class="line">// 3. 没有被frozen 冻结 也就是可扩展</span><br><span class="line">const canObserve = (value: Target): boolean =&gt; &#123;</span><br><span class="line">  return (</span><br><span class="line">    !value.__v_skip &amp;&amp;</span><br><span class="line">    isObservableType(toRawType(value)) &amp;&amp;</span><br><span class="line">    !Object.isFrozen(value)</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 可以被观察的值类型</span><br><span class="line">const isObservableType = /*#__PURE__*/ makeMap(</span><br><span class="line">  &#x27;Object,Array,Map,Set,WeakMap,WeakSet&#x27;</span><br><span class="line">)</span><br></pre></td></tr></table></figure></div>

<h2 id="ref"><a href="#ref" class="headerlink" title="ref"></a>ref</h2><p><code>createRef</code> 先判断 <code>value</code> 是否已经是一个 <code>ref</code>, 如果是则直接返回，如果不是接着判断是不是浅观察，如果是浅观察直接构造一个 <code>ref</code> 返回，不是则将 <code>rawValue</code> 转换成 <code>reactive</code> 再构造一个 <code>ref</code> 返回</p>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">export function ref(value?: unknown) &#123;</span><br><span class="line">  return createRef(value)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @description: </span><br><span class="line"> * @param &#123;rawValue&#125; 原始值 </span><br><span class="line"> * @param &#123;shallow&#125; 是否是浅观察 </span><br><span class="line"> */</span><br><span class="line">function createRef(rawValue: unknown, shallow = false) &#123;</span><br><span class="line">  // 如果已经是ref直接返回</span><br><span class="line">  if (isRef(rawValue)) &#123;</span><br><span class="line">    return rawValue</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // 如果是浅观察直接观察，不是则将 rawValue 转换成 reactive ,</span><br><span class="line">  // reactive 的定义在下方 </span><br><span class="line">  let value = shallow ? rawValue : convert(rawValue)</span><br><span class="line"></span><br><span class="line">  // ref 的结构</span><br><span class="line">  const r = &#123;</span><br><span class="line">    // ref 标识</span><br><span class="line">    __v_isRef: true,</span><br><span class="line">    get value() &#123;</span><br><span class="line">      // 依赖收集</span><br><span class="line">      track(r, TrackOpTypes.GET, &#x27;value&#x27;)</span><br><span class="line">      return value</span><br><span class="line">    &#125;,</span><br><span class="line">    set value(newVal) &#123;</span><br><span class="line">      if (hasChanged(toRaw(newVal), rawValue)) &#123;</span><br><span class="line">        rawValue = newVal</span><br><span class="line">        value = shallow ? newVal : convert(newVal)</span><br><span class="line">        // 触发依赖</span><br><span class="line">        trigger(</span><br><span class="line">          r,</span><br><span class="line">          TriggerOpTypes.SET,</span><br><span class="line">          &#x27;value&#x27;,</span><br><span class="line">          __DEV__ ? &#123; newValue: newVal &#125; : void 0</span><br><span class="line">        )</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  return r</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 如是是对象则调用 reactive, 否则直接返回 </span><br><span class="line">const convert = &lt;T extends unknown&gt;(val: T): T =&gt;</span><br><span class="line">  isObject(val) ? reactive(val) : val</span><br></pre></td></tr></table></figure></div>

<h2 id="BaseHandlers"><a href="#BaseHandlers" class="headerlink" title="BaseHandlers"></a>BaseHandlers</h2><ul>
<li>mutableHandlers 可变处理</li>
<li>readonlyHandlers 只读处理</li>
<li>shallowReactiveHandlers 浅观察处理（只观察目标对象的第一层属性）</li>
<li>shallowReadonlyHandlers 浅观察 &amp;&amp; 只读处理</li>
</ul>
<p>mutableHandlers源码为主</p>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">export const mutableHandlers: ProxyHandler&lt;object&gt; = &#123;</span><br><span class="line">  get,</span><br><span class="line">  set,</span><br><span class="line">  deleteProperty,</span><br><span class="line">  has,</span><br><span class="line">  ownKeys</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>这里面主要做的就是对数据的简单处理，对响应数据的订阅以及dispatch</p>
<h3 id="get"><a href="#get" class="headerlink" title="get"></a>get</h3><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">function createGetter(isReadonly = false, shallow = false) &#123;</span><br><span class="line">  return function get(target: Target, key: string | symbol, receiver: object) &#123;</span><br><span class="line">    if (key === ReactiveFlags.IS_REACTIVE) &#123;</span><br><span class="line">      return !isReadonly</span><br><span class="line">    &#125; else if (key === ReactiveFlags.IS_READONLY) &#123;</span><br><span class="line">      return isReadonly</span><br><span class="line">    &#125; else if (key === ReactiveFlags.IS_SHALLOW) &#123; // 这几行主要拿标签 内部用的较多</span><br><span class="line">      return shallow</span><br><span class="line">    &#125; else if (</span><br><span class="line">      key === ReactiveFlags.RAW &amp;&amp; // 这个看不懂</span><br><span class="line">      receiver ===</span><br><span class="line">        (isReadonly</span><br><span class="line">          ? shallow</span><br><span class="line">            ? shallowReadonlyMap</span><br><span class="line">            : readonlyMap</span><br><span class="line">          : shallow</span><br><span class="line">          ? shallowReactiveMap</span><br><span class="line">          : reactiveMap</span><br><span class="line">        ).get(target)</span><br><span class="line">    ) &#123;</span><br><span class="line">      return target</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    const targetIsArray = isArray(target) </span><br><span class="line"></span><br><span class="line">    if (!isReadonly) &#123;</span><br><span class="line">      if (targetIsArray &amp;&amp; hasOwn(arrayInstrumentations, key)) &#123; </span><br><span class="line">        // 如果目标对象是数组并且 key 属于三个方法之一 [&#x27;includes&#x27;, &#x27;indexOf&#x27;, &#x27;lastIndexOf&#x27;]，即触发了这三个操作之一  arrayInstrumentations里面内部实现了订阅 track</span><br><span class="line">        return Reflect.get(arrayInstrumentations, key, receiver)</span><br><span class="line">      &#125;</span><br><span class="line">      if (key === &#x27;hasOwnProperty&#x27;) &#123;</span><br><span class="line">        return hasOwnProperty // 同上</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    const res = Reflect.get(target, key, receiver)</span><br><span class="line"></span><br><span class="line">// 如果 key 是 symbol 内置方法，或者访问的是原型对象，直接返回结果，不收集依赖</span><br><span class="line">    if (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) &#123;</span><br><span class="line">      return res</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    if (!isReadonly) &#123;</span><br><span class="line">      track(target, TrackOpTypes.GET, key)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    if (shallow) &#123;</span><br><span class="line">      return res</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    if (isRef(res)) &#123;</span><br><span class="line">      // ref unwrapping - skip unwrap for Array + integer key.</span><br><span class="line">      return targetIsArray &amp;&amp; isIntegerKey(key) ? res : res.value</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    // 由于 proxy 只能代理一层，所以 target[key] 的值如果是对象，就继续对其进行代理 递归</span><br><span class="line">    if (isObject(res)) &#123;</span><br><span class="line">      // Convert returned value into a proxy as well. we do the isObject check</span><br><span class="line">      // here to avoid invalid value warning. Also need to lazy access readonly</span><br><span class="line">      // and reactive here to avoid circular dependency.</span><br><span class="line">      return isReadonly ? readonly(res) : reactive(res)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return res</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>

<h3 id="set"><a href="#set" class="headerlink" title="set"></a>set</h3><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">function createSetter(shallow = false) &#123;</span><br><span class="line">  return function set(</span><br><span class="line">    target: object,</span><br><span class="line">    key: string | symbol,</span><br><span class="line">    value: unknown,</span><br><span class="line">    receiver: object</span><br><span class="line">  ): boolean &#123;</span><br><span class="line">    let oldValue = (target as any)[key]</span><br><span class="line">    if (isReadonly(oldValue) &amp;&amp; isRef(oldValue) &amp;&amp; !isRef(value)) &#123; //只读</span><br><span class="line">      return false</span><br><span class="line">    &#125;</span><br><span class="line">    if (!shallow) &#123;</span><br><span class="line">      if (!isShallow(value) &amp;&amp; !isReadonly(value)) &#123;</span><br><span class="line">        oldValue = toRaw(oldValue) // toRaw 这玩意干啥的</span><br><span class="line">        value = toRaw(value)</span><br><span class="line">      &#125;</span><br><span class="line">      if (!isArray(target) &amp;&amp; isRef(oldValue) &amp;&amp; !isRef(value)) &#123;</span><br><span class="line">        oldValue.value = value</span><br><span class="line">        return true</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      // in shallow mode, objects are set as-is regardless of reactive or not</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">//  处理数组 判断是修改还是增加 并 dispatch</span><br><span class="line">    const hadKey =</span><br><span class="line">      isArray(target) &amp;&amp; isIntegerKey(key)</span><br><span class="line">        ? Number(key) &lt; target.length</span><br><span class="line">        : hasOwn(target, key)</span><br><span class="line">    const result = Reflect.set(target, key, value, receiver)</span><br><span class="line">    // don&#x27;t trigger if target is something up in the prototype chain of original</span><br><span class="line">    if (target === toRaw(receiver)) &#123;</span><br><span class="line">      if (!hadKey) &#123;</span><br><span class="line">        trigger(target, TriggerOpTypes.ADD, key, value)</span><br><span class="line">      &#125; else if (hasChanged(value, oldValue)) &#123;</span><br><span class="line">        trigger(target, TriggerOpTypes.SET, key, value, oldValue)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return result</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<h1 id="Effect"><a href="#Effect" class="headerlink" title="Effect"></a>Effect</h1><h2 id="effect-难"><a href="#effect-难" class="headerlink" title="effect 难"></a>effect 难</h2><p>用react会比较熟悉，react叫副作用 vue叫事件，但是作用都是差不多，都是在依赖变化的时候调用内部的方法</p>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">export function effect&lt;T = any&gt;(</span><br><span class="line">  fn: () =&gt; T,</span><br><span class="line">  options?: ReactiveEffectOptions</span><br><span class="line">): ReactiveEffectRunner &#123;</span><br><span class="line">  if ((fn as ReactiveEffectRunner).effect) &#123;</span><br><span class="line">    fn = (fn as ReactiveEffectRunner).effect.fn</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  const _effect = new ReactiveEffect(fn)</span><br><span class="line">  if (options) &#123;</span><br><span class="line">    extend(_effect, options)</span><br><span class="line">    if (options.scope) recordEffectScope(_effect, options.scope)</span><br><span class="line">  &#125;</span><br><span class="line">  if (!options || !options.lazy) &#123;</span><br><span class="line">    _effect.run()</span><br><span class="line">  &#125;</span><br><span class="line">  const runner = _effect.run.bind(_effect) as ReactiveEffectRunner</span><br><span class="line">  runner.effect = _effect</span><br><span class="line">  return runner</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// ReactiveEffect 这个类的真实代码比较难读，换一个网站的简易代码，但是内部的真实意思大差不差</span><br><span class="line"></span><br><span class="line">function createReactiveEffect&lt;T = any&gt;(</span><br><span class="line">  fn: (...args: any[]) =&gt; T,</span><br><span class="line">  options: ReactiveEffectOptions</span><br><span class="line">): ReactiveEffect&lt;T&gt; &#123;</span><br><span class="line">  const effect = function reactiveEffect(...args: unknown[]): unknown &#123;</span><br><span class="line"></span><br><span class="line">    // 没有激活，说明我们调用了effect stop 函数，</span><br><span class="line">    if (!effect.active) &#123;</span><br><span class="line">      // 如果没有调度者，直接返回，否则直接执行fn</span><br><span class="line">      return options.scheduler ? undefined : fn(...args)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    // 判断effectStack中有没有effect, 如果在则不处理</span><br><span class="line">    if (!effectStack.includes(effect)) &#123;</span><br><span class="line">      // 清除effect依赖，定义在下方</span><br><span class="line">      cleanup(effect)</span><br><span class="line">      try &#123;</span><br><span class="line">        // 开始重新收集依赖</span><br><span class="line">        enableTracking()</span><br><span class="line">        // 压入Stack</span><br><span class="line">        effectStack.push(effect)</span><br><span class="line">        // 将activeEffect当前effect </span><br><span class="line">        activeEffect = effect</span><br><span class="line">        return fn(...args)</span><br><span class="line">      &#125; finally &#123;</span><br><span class="line">        // 完成后将effect弹出</span><br><span class="line">        effectStack.pop()</span><br><span class="line">        // 重置依赖</span><br><span class="line">        resetTracking()</span><br><span class="line">        // 重置activeEffect </span><br><span class="line">        activeEffect = effectStack[effectStack.length - 1]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; as ReactiveEffect</span><br><span class="line">  effect.id = uid++ // 自增id, effect唯一标识</span><br><span class="line">  effect._isEffect = true  // 是否是effect</span><br><span class="line">  effect.active = true // 是否激活 </span><br><span class="line">  effect.raw = fn // 挂载原始对象</span><br><span class="line">  effect.deps = []  // 当前 effect 的dep 数组</span><br><span class="line">  effect.options = options // 传入的options，在effect有解释的那个字段</span><br><span class="line">  return effect</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const effectStack: ReactiveEffect[] = []</span><br><span class="line"></span><br><span class="line">// 每次 effect 运行都会重新收集依赖, deps 是 effect 的依赖数组, 需要全部清空</span><br><span class="line">function cleanup(effect: ReactiveEffect) &#123;</span><br><span class="line">  const &#123; deps &#125; = effect</span><br><span class="line">  if (deps.length) &#123;</span><br><span class="line">    for (let i = 0; i &lt; deps.length; i++) &#123;</span><br><span class="line">      deps[i].delete(effect)</span><br><span class="line">    &#125;</span><br><span class="line">    deps.length = 0</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>

<h2 id="track-收集依赖"><a href="#track-收集依赖" class="headerlink" title="track 收集依赖"></a>track 收集依赖</h2><p>在 <code>baseHandles</code> 会在代理中频繁出现此方法，就是收集依赖，然后再去触发<br>总结下 就是将将要改变的 <code>key</code> 和 <code>ReactiveEffect</code> 放到targetMap 中收集起来，等到变化的时候再去调用原始的fn方法</p>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">export function track(target: object, type: TrackOpTypes, key: unknown) &#123;</span><br><span class="line">  if (shouldTrack &amp;&amp; activeEffect) &#123;</span><br><span class="line">    let depsMap = targetMap.get(target)</span><br><span class="line">    if (!depsMap) &#123;</span><br><span class="line">      targetMap.set(target, (depsMap = new Map()))</span><br><span class="line">    &#125;</span><br><span class="line">    let dep = depsMap.get(key)</span><br><span class="line">    if (!dep) &#123;</span><br><span class="line">      depsMap.set(key, (dep = createDep()))</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    const eventInfo = __DEV__</span><br><span class="line">      ? &#123; effect: activeEffect, target, type, key &#125;</span><br><span class="line">      : undefined</span><br><span class="line"></span><br><span class="line">    trackEffects(dep, eventInfo)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export function trackEffects(</span><br><span class="line">  dep: Dep,</span><br><span class="line">  debuggerEventExtraInfo?: DebuggerEventExtraInfo</span><br><span class="line">) &#123;</span><br><span class="line">  let shouldTrack = false</span><br><span class="line">  if (effectTrackDepth &lt;= maxMarkerBits) &#123;</span><br><span class="line">    if (!newTracked(dep)) &#123;</span><br><span class="line">      dep.n |= trackOpBit // set newly tracked</span><br><span class="line">      shouldTrack = !wasTracked(dep)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    // Full cleanup mode.</span><br><span class="line">    shouldTrack = !dep.has(activeEffect!)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  if (shouldTrack) &#123;</span><br><span class="line">    dep.add(activeEffect!)</span><br><span class="line">    activeEffect!.deps.push(dep)</span><br><span class="line">    if (__DEV__ &amp;&amp; activeEffect!.onTrack) &#123;</span><br><span class="line">      activeEffect!.onTrack(</span><br><span class="line">        extend(</span><br><span class="line">          &#123;</span><br><span class="line">            effect: activeEffect!</span><br><span class="line">          &#125;,</span><br><span class="line">          debuggerEventExtraInfo!</span><br><span class="line">        )</span><br><span class="line">      )</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>

<h2 id="trigger-触发依赖修改，通知"><a href="#trigger-触发依赖修改，通知" class="headerlink" title="trigger 触发依赖修改，通知"></a>trigger 触发依赖修改，通知</h2><p>增删改等操作后，<code>proxy</code> 触发 <code>trigger</code> 方法, 然后去触发deps中的 <code>ReactiveEffect</code> 中的fn</p>
<blockquote>
<p>trigger(target, TriggerOpTypes.ADD, key, value)</p>
</blockquote>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">export function trigger(</span><br><span class="line">  target: object,</span><br><span class="line">  type: TriggerOpTypes,</span><br><span class="line">  key?: unknown,</span><br><span class="line">  newValue?: unknown,</span><br><span class="line">  oldValue?: unknown,</span><br><span class="line">  oldTarget?: Map&lt;unknown, unknown&gt; | Set&lt;unknown&gt;</span><br><span class="line">) &#123;</span><br><span class="line">  const depsMap = targetMap.get(target)</span><br><span class="line">  if (!depsMap) &#123;</span><br><span class="line">    // never been tracked</span><br><span class="line">    return</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  let deps: (Dep | undefined)[] = []</span><br><span class="line">  if (type === TriggerOpTypes.CLEAR) &#123;</span><br><span class="line">    // collection being cleared</span><br><span class="line">    // trigger all effects for target</span><br><span class="line">    deps = [...depsMap.values()]</span><br><span class="line">  &#125; else if (key === &#x27;length&#x27; &amp;&amp; isArray(target)) &#123;</span><br><span class="line">    const newLength = Number(newValue)</span><br><span class="line">    depsMap.forEach((dep, key) =&gt; &#123;</span><br><span class="line">      if (key === &#x27;length&#x27; || key &gt;= newLength) &#123;</span><br><span class="line">        deps.push(dep)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    // schedule runs for SET | ADD | DELETE</span><br><span class="line">    if (key !== void 0) &#123;</span><br><span class="line">      deps.push(depsMap.get(key))</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    // also run for iteration key on ADD | DELETE | Map.SET</span><br><span class="line">    switch (type) &#123;</span><br><span class="line">      case TriggerOpTypes.ADD:</span><br><span class="line">        if (!isArray(target)) &#123;</span><br><span class="line">          deps.push(depsMap.get(ITERATE_KEY))</span><br><span class="line">          if (isMap(target)) &#123;</span><br><span class="line">            deps.push(depsMap.get(MAP_KEY_ITERATE_KEY))</span><br><span class="line">          &#125;</span><br><span class="line">        &#125; else if (isIntegerKey(key)) &#123;</span><br><span class="line">          // new index added to array -&gt; length changes</span><br><span class="line">          deps.push(depsMap.get(&#x27;length&#x27;))</span><br><span class="line">        &#125;</span><br><span class="line">        break</span><br><span class="line">      case TriggerOpTypes.DELETE:</span><br><span class="line">        if (!isArray(target)) &#123;</span><br><span class="line">          deps.push(depsMap.get(ITERATE_KEY))</span><br><span class="line">          if (isMap(target)) &#123;</span><br><span class="line">            deps.push(depsMap.get(MAP_KEY_ITERATE_KEY))</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        break</span><br><span class="line">      case TriggerOpTypes.SET:</span><br><span class="line">        if (isMap(target)) &#123;</span><br><span class="line">          deps.push(depsMap.get(ITERATE_KEY))</span><br><span class="line">        &#125;</span><br><span class="line">        break</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  const eventInfo = __DEV__</span><br><span class="line">    ? &#123; target, type, key, newValue, oldValue, oldTarget &#125;</span><br><span class="line">    : undefined</span><br><span class="line"></span><br><span class="line">  if (deps.length === 1) &#123;</span><br><span class="line">    if (deps[0]) &#123;</span><br><span class="line">      if (__DEV__) &#123;</span><br><span class="line">        triggerEffects(deps[0], eventInfo)</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        triggerEffects(deps[0])</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    const effects: ReactiveEffect[] = []</span><br><span class="line">    for (const dep of deps) &#123;</span><br><span class="line">      if (dep) &#123;</span><br><span class="line">        effects.push(...dep)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    if (__DEV__) &#123;</span><br><span class="line">      triggerEffects(createDep(effects), eventInfo)</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      triggerEffects(createDep(effects))</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export function triggerEffects(</span><br><span class="line">  dep: Dep | ReactiveEffect[],</span><br><span class="line">  debuggerEventExtraInfo?: DebuggerEventExtraInfo</span><br><span class="line">) &#123;</span><br><span class="line">  // spread into array for stabilization</span><br><span class="line">  const effects = isArray(dep) ? dep : [...dep]</span><br><span class="line">  for (const effect of effects) &#123;</span><br><span class="line">    if (effect.computed) &#123;</span><br><span class="line">      triggerEffect(effect, debuggerEventExtraInfo)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  for (const effect of effects) &#123;</span><br><span class="line">    if (!effect.computed) &#123;</span><br><span class="line">      triggerEffect(effect, debuggerEventExtraInfo)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function triggerEffect(</span><br><span class="line">  effect: ReactiveEffect,</span><br><span class="line">  debuggerEventExtraInfo?: DebuggerEventExtraInfo</span><br><span class="line">) &#123;</span><br><span class="line">  if (effect !== activeEffect || effect.allowRecurse) &#123;</span><br><span class="line">    if (__DEV__ &amp;&amp; effect.onTrigger) &#123;</span><br><span class="line">      effect.onTrigger(extend(&#123; effect &#125;, debuggerEventExtraInfo))</span><br><span class="line">    &#125;</span><br><span class="line">    if (effect.scheduler) &#123;</span><br><span class="line">      effect.scheduler()</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      effect.run()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export function getDepFromReactive(object: any, key: string | number | symbol) &#123;</span><br><span class="line">  return targetMap.get(object)?.get(key)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>

<h1 id="computed"><a href="#computed" class="headerlink" title="computed"></a>computed</h1><blockquote>
<p>用法：const plusOne &#x3D; computed(() &#x3D;&gt; count.value + 1)<br>设置代理和effect 相结合</p>
</blockquote>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">export function computed&lt;T&gt;(</span><br><span class="line">  getterOrOptions: ComputedGetter&lt;T&gt; | WritableComputedOptions&lt;T&gt;</span><br><span class="line">) &#123;</span><br><span class="line">  let getter: ComputedGetter&lt;T&gt;</span><br><span class="line">  let setter: ComputedSetter&lt;T&gt;</span><br><span class="line"></span><br><span class="line">  // 如果传入是 function 说明是只读 computed</span><br><span class="line">  if (isFunction(getterOrOptions)) &#123;</span><br><span class="line">    getter = getterOrOptions</span><br><span class="line">    setter = __DEV__</span><br><span class="line">      ? () =&gt; &#123;</span><br><span class="line">          console.warn(&#x27;Write operation failed: computed value is readonly&#x27;)</span><br><span class="line">        &#125;</span><br><span class="line">      : NOOP</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    // 不是方法说明是自定义的 getter setter </span><br><span class="line">    getter = getterOrOptions.get</span><br><span class="line">    setter = getterOrOptions.set</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  let dirty = true</span><br><span class="line">  let value: T</span><br><span class="line">  let computed: ComputedRef&lt;T&gt;</span><br><span class="line"></span><br><span class="line">  // 创建 effect, 我们在看 effect 源码时知道了传入 lazy 代表不会立即执行，computed 表明 computed 上游依赖改变的时候，会优先 trigger runner effect, scheduler 表示 effect trigger 的时候会调用 scheduler 而不是直接调用 effect</span><br><span class="line">  const runner = effect(getter, &#123;</span><br><span class="line">    lazy: true,</span><br><span class="line">    // mark effect as computed so that it gets priority during trigger</span><br><span class="line">    computed: true,</span><br><span class="line">    scheduler: () =&gt; &#123;</span><br><span class="line">      // 在触发更新时把dirty置为true, 不会立即更新 </span><br><span class="line">      if (!dirty) &#123;</span><br><span class="line">        dirty = true</span><br><span class="line">        trigger(computed, TriggerOpTypes.SET, &#x27;value&#x27;)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  // 构造一个 computed 返回</span><br><span class="line">  computed = &#123;</span><br><span class="line">    __v_isRef: true,</span><br><span class="line">    // expose effect so computed can be stopped</span><br><span class="line">    effect: runner,</span><br><span class="line">    get value() &#123;</span><br><span class="line">      // dirty为ture, get操作时，执行effect获取最新值</span><br><span class="line">      // </span><br><span class="line">      if (dirty) &#123;</span><br><span class="line">        value = runner()</span><br><span class="line">        dirty = false</span><br><span class="line">      &#125;</span><br><span class="line">      // dirty为false, 表示值未更新，直接返回 </span><br><span class="line">      track(computed, TrackOpTypes.GET, &#x27;value&#x27;)</span><br><span class="line">      return value</span><br><span class="line">    &#125;,</span><br><span class="line">    set value(newValue: T) &#123;</span><br><span class="line">      setter(newValue)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; as any</span><br><span class="line">  return computed</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
        </div>

        
            <div class="post-copyright-info">
                <div class="article-copyright-info-container">
    <ul>
        <li><strong>标题:</strong> vue响应式系统</li>
        <li><strong>作者:</strong> jiesun</li>
        <li><strong>创建于
                :</strong> 2023-02-21 13:59:18</li>
        
            <li>
                <strong>更新于
                    :</strong> 2023-02-27 16:12:04
            </li>
        
        <li>
            <strong>链接:</strong> https://anca.gitee.io/jiesun.gitee.io/2023/02/21/reactive/
        </li>
        <li>
            <strong>
                版权声明:
            </strong>
            
            本文章采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0</a> 进行许可。
            

        </li>
    </ul>
</div>

            </div>
        

        

        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                        rel="prev"
                        href="/jiesun.gitee.io/2023/03/22/webpack/"
                        >
                            <span class="left arrow-icon flex justify-center items-center">
                                <i class="fa-solid fa-chevron-left"></i>
                            </span>
                            <span class="title flex justify-center items-center">
                                <span class="post-nav-title-item">webpack的一些使用</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                        rel="next"
                        href="/jiesun.gitee.io/2023/02/17/vue-nexttick/"
                        >
                            <span class="title flex justify-center items-center">
                                <span class="post-nav-title-item">vue事件列表和组件更新</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex justify-center items-center">
                                <i class="fa-solid fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        


        
    </div>

    
        <div class="toc-content-container">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">此页目录</div>
        <div class="page-title">vue响应式系统</div>
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#reactive"><span class="nav-text">reactive</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ref"><span class="nav-text">ref</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#BaseHandlers"><span class="nav-text">BaseHandlers</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#get"><span class="nav-text">get</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#set"><span class="nav-text">set</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Effect"><span class="nav-text">Effect</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#effect-%E9%9A%BE"><span class="nav-text">effect 难</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#track-%E6%94%B6%E9%9B%86%E4%BE%9D%E8%B5%96"><span class="nav-text">track 收集依赖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#trigger-%E8%A7%A6%E5%8F%91%E4%BE%9D%E8%B5%96%E4%BF%AE%E6%94%B9%EF%BC%8C%E9%80%9A%E7%9F%A5"><span class="nav-text">trigger 触发依赖修改，通知</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#computed"><span class="nav-text">computed</span></a>

    </div>
</div>
        </div>
    
</div>



                

            </div>

            

        </div>

        <div class="main-content-footer">
            <footer class="footer mt-5 py-5 h-auto text-base text-third-text-color relative border-t-2 border-t-border-color">
    <div class="info-container py-3 text-center">
        
        <div class="text-center">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">jiesun</a>
        </div>
        
            <script data-swup-reload-script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="relative text-center lg:absolute lg:right-[20px] lg:top-1/2 lg:-translate-y-1/2 lg:text-right">
                
                    <span id="busuanzi_container_site_uv" class="lg:!block">
                        <span class="text-sm">访问人数</span>
                        <span id="busuanzi_value_site_uv"></span>
                    </span>
                
                
                    <span id="busuanzi_container_site_pv" class="lg:!block">
                        <span class="text-sm">总访问量</span>
                        <span id="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="relative text-center lg:absolute lg:left-[20px] lg:top-1/2 lg:-translate-y-1/2 lg:text-left">
            <span class="lg:block text-sm">由 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg class="relative top-[2px] inline-block align-baseline" version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" class="text-base" href="https://hexo.io">Hexo</a> 驱动</span>
            <span class="text-sm lg:block">主题&nbsp;<a class="text-base" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.4.4</a></span>
        </div>
        
        
            <div>
                博客已运行 <span class="odometer" id="runtime_days" ></span> 天 <span class="odometer" id="runtime_hours"></span> 小时 <span class="odometer" id="runtime_minutes"></span> 分钟 <span class="odometer" id="runtime_seconds"></span> 秒
            </div>
        
        
            <script data-swup-reload-script>
                try {
                    function odometer_init() {
                    const elements = document.querySelectorAll('.odometer');
                    elements.forEach(el => {
                        new Odometer({
                            el,
                            format: '( ddd).dd',
                            duration: 200
                        });
                    });
                    }
                    odometer_init();
                } catch (error) {}
            </script>
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex justify-center items-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex justify-center items-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-expand-width flex justify-center items-center">
            <i class="fa-regular fa-expand"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex justify-center items-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex justify-center items-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex justify-center items-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex justify-center items-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    

</main>


    
<script src="/jiesun.gitee.io/js/libs/Swup.min.js"></script>

<script src="/jiesun.gitee.io/js/libs/SwupSlideTheme.min.js"></script>

<script src="/jiesun.gitee.io/js/libs/SwupScriptsPlugin.min.js"></script>

<script src="/jiesun.gitee.io/js/libs/SwupProgressPlugin.min.js"></script>

<script src="/jiesun.gitee.io/js/libs/SwupScrollPlugin.min.js"></script>

<script>
    const swup = new Swup({
        plugins: [
            new SwupScriptsPlugin({
                optin: true,
            }),
            new SwupProgressPlugin(),
            new SwupScrollPlugin({
                offset: 80,
            }),
            new SwupSlideTheme({
                mainElement: ".main-content-body",
            }),
        ],
        containers: ["#swup"],
    });

    swup.hooks.on("page:view", () => {
        Global.refresh();
    });

    // if (document.readyState === "complete") {
    //
    // } else {
    //     document.addEventListener("DOMContentLoaded", () => init());
    // }
</script>






<script src="/jiesun.gitee.io/js/utils.js" type="module"></script>

<script src="/jiesun.gitee.io/js/main.js" type="module"></script>

<script src="/jiesun.gitee.io/js/layouts/navbarShrink.js" type="module"></script>

<script src="/jiesun.gitee.io/js/tools/scrollTopBottom.js" type="module"></script>

<script src="/jiesun.gitee.io/js/tools/lightDarkSwitch.js" type="module"></script>

<script src="/jiesun.gitee.io/js/layouts/categoryList.js" type="module"></script>





    
<script src="/jiesun.gitee.io/js/tools/codeBlock.js"></script>




    
<script src="/jiesun.gitee.io/js/layouts/lazyload.js"></script>




    
<script src="/jiesun.gitee.io/js/tools/runtime.js"></script>

    
<script src="/jiesun.gitee.io/js/libs/odometer.min.js"></script>

    
<link rel="stylesheet" href="/jiesun.gitee.io/assets/odometer-theme-minimal.css">




  
<script src="/jiesun.gitee.io/js/libs/Typed.min.js"></script>

  
<script src="/jiesun.gitee.io/js/plugins/typed.js"></script>







<div class="post-scripts" data-swup-reload-script>
    
        
<script src="/jiesun.gitee.io/js/libs/anime.min.js"></script>

        
<script src="/jiesun.gitee.io/js/tools/tocToggle.js" type="module"></script>

<script src="/jiesun.gitee.io/js/layouts/toc.js" type="module"></script>

<script src="/jiesun.gitee.io/js/plugins/tabs.js" type="module"></script>

    
</div>


    <div id="aplayer"></div>

<script src="/jiesun.gitee.io/js/libs/APlayer.min.js"></script>


<script src="/jiesun.gitee.io/js/plugins/aplayer.js"></script>


</body>
</html>
